﻿using System.Collections.Generic;

namespace dk.Helper.Base.Core
{
    public class VillageGroups : IVillageGroups
    {
        public VillageGroups(IVillage village)
        {
            Village = village;
            Groups = new Dictionary<string, IVillageGroup>();
        }

        public IVillage Village { get; private set; }

        public IDictionary<string, IVillageGroup> Groups { get; private set; }
        
        public void AddGroup(IVillageGroup villageGroup)
        {
            if (this.Groups.ContainsKey(villageGroup.Name)) return;
            this.Groups.Add(villageGroup.Name, villageGroup);
            villageGroup.AddVillage(this.Village);
        }

        public bool Contains(IVillageGroup villageGroup)
        {
            return Groups.ContainsKey(villageGroup.Name);
        }

        public void RemoveGroup(IVillageGroup villageGroup)
        {
            RemoveGroup(villageGroup.Name);
        }

        public void RemoveGroup(string groupName)
        {
            if (!this.Groups.ContainsKey(groupName)) return;
            this.Groups[groupName].RemoveVillage(this.Village);
            this.Groups.Remove(groupName);
        }

        public IList<IVillageGroup> GetGroupsByOrder(int order)
        {
            IList<IVillageGroup> villageGroupList = new List<IVillageGroup>();
            foreach (IVillageGroup villageGroup in Groups.Values)
            {
                if (villageGroup.GroupType.Order != order || villageGroupList.Contains(villageGroup)) continue;
                
                villageGroupList.Add(villageGroup);
            }
            return villageGroupList;
        }
    }
}